package com.furentech.iotserver.fa.sinopec.sinopec_processor;


import com.furentech.iotserver.fa.server.ChannelUtil;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.sinopec.SinopecParam;
import com.furentech.iotserver.fa.sinopec.SinopecRestRequest;
import com.furentech.iotserver.fa.utils.HexStringUtils;
import com.furentech.iotserver.fa.utils.SpringUtil;
import io.netty.channel.Channel;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
class AuthReq2
{
    public AuthReq2() {
        POS_TTC = new byte[4];
        MAC = new byte[4];
        ASN = new byte[10];

    }

    byte NZN;
    byte[] POS_TTC;
    String DispenserT_IME;
    byte Pay_X;
    int PRC;
    int AMN;
    int VOL;
    byte[] ASN;
    byte[] MAC;
}


@Component
public class AuthReq2_Processor extends SinopecProcessorBase {

    AuthReq2  decodeFormBytes(byte[] buf) {
        AuthReq2 authReq2 = new AuthReq2();

        authReq2.setNZN(buf[1]);
        System.arraycopy(buf, 2, authReq2.POS_TTC, 0, 4);
        authReq2.setDispenserT_IME(HexStringUtils.toHexString(buf, 6, 7));
        authReq2.setPay_X(buf[13]);
        authReq2.setPRC((0x00ff & buf[14]) * 256 + (0x00ff & buf[15]));
        authReq2.setAMN((0x00ff & buf[16]) * 65536 + (0x00ff & buf[17]) * 256 + (0x00ff & buf[18]));
        authReq2.setVOL((0x00ff & buf[19]) * 65536 + (0x00ff & buf[20]) * 256 + (0x00ff & buf[21]));
        System.arraycopy(buf, 22, authReq2.ASN, 0, 10);
        System.arraycopy(buf, 32, authReq2.MAC, 0, 4);

        return  authReq2;
    }



    @Override
    String actualGetJsonString(byte[] buf) {
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        return "员工卡授权请求";
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {
        AuthReq2 authReq2 = decodeFormBytes(buf);

        SinopecParam sinopecParam = SpringUtil.getBean(SinopecParam.class);

        byte[] responseBuf;
        if (sinopecParam.addIn) {
            responseBuf = new byte[30];
        } else {
            responseBuf = new byte[28];
        }



        responseBuf[0]= SinopecFunCode.Auth_FB & 0x00ff;
        responseBuf[1] = authReq2.getNZN();
        System.arraycopy(authReq2.POS_TTC, 0, responseBuf, 2, 4);
        responseBuf[6] = authReq2.getPay_X();
        responseBuf[7] = (byte) (0x00ff & (authReq2.getPRC() / 256));
        responseBuf[8] = (byte) (0x00ff & (authReq2.getPRC() % 256));



        if (sinopecParam.addIn) {

            responseBuf[9]=2;
            responseBuf[10]='O';
            responseBuf[11] = 'K';

           if(authReq2.getAMN()==0)
           {
               int amnAuth;

               if (authReq2.getVOL() == 0) {


                   amnAuth=999900;

                   responseBuf[12]=(byte) (0x00ff & (amnAuth / 65536));
                   responseBuf[13]=(byte) (0x00ff & (amnAuth / 256));
                   responseBuf[14] = (byte) (0x00ff & (amnAuth % 256));
               }else
               {
                   amnAuth = authReq2.getVOL() * authReq2.getPRC();
                   responseBuf[12]=(byte) (0x00ff & (amnAuth / 65536));
                   responseBuf[13]=(byte) (0x00ff & (amnAuth / 256));
                   responseBuf[14] = (byte) (0x00ff & (amnAuth % 256));
               }
           }
           else
           {
               responseBuf[12]=(byte) (0x00ff & (authReq2.getAMN() / 65536));
               responseBuf[13]=(byte) (0x00ff & (authReq2.getAMN() / 256));
               responseBuf[14] = (byte) (0x00ff & (authReq2.getAMN() % 256));
           }



            System.arraycopy(authReq2.getASN(), 0, responseBuf, 15, 10);

            if (SinopecRestRequest.getWorkCardAbility(HexStringUtils.toHexString(authReq2.ASN, 0, 10))) {
                responseBuf[25] = 0x01;
            } else {
                responseBuf[25] = 0x03;
            }
            System.arraycopy(authReq2.MAC, 0, responseBuf, 26, 4);

            Byte[] tempBuf = new Byte[responseBuf.length];

            for (int i = 0; i < responseBuf.length;i++) {
                tempBuf[i] = responseBuf[i];
            }

            ChannelUtil.setAuthReq2RespBuf(channel,tempBuf);
        }else {


            responseBuf[9] = 0;

            if (authReq2.getAMN() == 0) {
                int amnAuth;

                if (authReq2.getVOL() == 0) {

                    amnAuth = 999900;

                    responseBuf[10] = (byte) (0x00ff & (amnAuth / 65536));
                    responseBuf[11] = (byte) (0x00ff & (amnAuth / 256));
                    responseBuf[12] = (byte) (0x00ff & (amnAuth % 256));
                } else {
                    amnAuth = authReq2.getVOL() * authReq2.getPRC();
                    responseBuf[10] = (byte) (0x00ff & (amnAuth / 65536));
                    responseBuf[11] = (byte) (0x00ff & (amnAuth / 256));
                    responseBuf[12] = (byte) (0x00ff & (amnAuth % 256));
                }
            } else {
                responseBuf[10] = (byte) (0x00ff & (authReq2.getAMN() / 65536));
                responseBuf[11] = (byte) (0x00ff & (authReq2.getAMN() / 256));
                responseBuf[12] = (byte) (0x00ff & (authReq2.getAMN() % 256));
            }


            System.arraycopy(authReq2.getASN(), 0, responseBuf, 13, 10);

            if (SinopecRestRequest.getWorkCardAbility(HexStringUtils.toHexString(authReq2.ASN, 0, 10))) {
                responseBuf[23] = 0x01;
            } else {
                responseBuf[23] = 0x03;
            }
            System.arraycopy(authReq2.MAC, 0, responseBuf, 24, 4);



            Byte[] tempBuf = new Byte[responseBuf.length];

            for (int i = 0; i < responseBuf.length;i++) {
                tempBuf[i] = responseBuf[i];
            }

            ChannelUtil.setAuthReq2RespBuf(channel,tempBuf);
        }

        ChannelUtil.setAuthReq2Flag(channel, true);

        byte[] resByteBuf = new byte[4];

        resByteBuf[0] = SinopecFunCode.CMD_ACK;
        resByteBuf[1] = buf[0];
        resByteBuf[2] = authReq2.getNZN();
        resByteBuf[3] = 0x02;

        return resByteBuf;
    }
}
